package com.atguigu.dga.governance.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.dga.governance.bean.GovernanceAssessDetail;
import com.atguigu.dga.governance.bean.GovernanceAssessGlobal;
import com.atguigu.dga.governance.bean.GovernanceAssessTecOwner;
import com.atguigu.dga.governance.service.GovernanceAssessDetailService;
import com.atguigu.dga.governance.service.GovernanceAssessGlobalService;
import com.atguigu.dga.governance.service.GovernanceAssessTecOwnerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 治理考评结果明细 前端控制器
 * </p>
 *
 * @author zhangjunyi
 * @since 2023-06-06
 */
@RestController
@RequestMapping(value = "/governance")
public class GovernanceAssessDetailController {

    @Autowired
    GovernanceAssessDetailService governanceAssessDetailService;

    @Autowired
    GovernanceAssessGlobalService governanceAssessGlobalService;

    @Autowired
    GovernanceAssessTecOwnerService governanceAssessTecOwnerService;


    //1.手动评估
    @PostMapping("/assess/{assessDate}")
    public String assess(@PathVariable("assessDate") String assessDate) {
        governanceAssessDetailService.mainAssess(assessDate);
        return "success";
    }

    //2.获取全局分数
    @GetMapping("/globalScore")
    public String globalScore() {
        //1.查询全局分数表  查询出来的是最新考评的一条数据
        QueryWrapper<GovernanceAssessGlobal> queryWrapper = new QueryWrapper<GovernanceAssessGlobal>()
                .orderByDesc("assess_date")
                .last("limit 1");
        GovernanceAssessGlobal governanceAssessGlobal = governanceAssessGlobalService.getOne(queryWrapper);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("assessDate", governanceAssessGlobal.getAssessDate());
        jsonObject.put("sumScore", governanceAssessGlobal.getScore());

        ArrayList<BigDecimal> sortList = new ArrayList<>();
        sortList.add(governanceAssessGlobal.getScoreSpec());
        sortList.add(governanceAssessGlobal.getScoreStorage());
        sortList.add(governanceAssessGlobal.getScoreCalc());
        sortList.add(governanceAssessGlobal.getScoreQuality());
        sortList.add(governanceAssessGlobal.getScoreSecurity());

        jsonObject.put("scoreList", sortList);

        return jsonObject.toJSONString();
    }

    //问题列表接口
    @GetMapping("/problemList/{governType}/{pageNo}/{pageSize}")
    public String prolemList(@PathVariable("governType") String governType,
                             @PathVariable("pageNo") Integer pageNo,
                             @PathVariable("pageSize") Integer pageSize) {

        Integer from = (pageNo - 1) * pageSize;

        //1.查询考评明细表
        QueryWrapper<GovernanceAssessDetail> queryWrapper = new QueryWrapper<GovernanceAssessDetail>()
                .eq("governance_type", governType)
                .lt("assess_score", 10)
                .inSql("assess_date", "select max(assess_date) from governance_assess_detail")
                .last(" limit " + from + "," + pageSize);

        List<GovernanceAssessDetail> governanceAssessDetails = governanceAssessDetailService.list(queryWrapper);

        return JSONObject.toJSONString(governanceAssessDetails);
    }

    //各个治理类型问题个数
    @GetMapping("/problemNum")
    public String problemNum(){
        QueryWrapper<GovernanceAssessDetail> queryWrapper = new QueryWrapper<GovernanceAssessDetail>()
                .select("governance_type", "count(*) as ct")
                .lt("assess_score", 10)
                .inSql("assess_date", "select max(assess_date) from governance_assess_detail")
                .last(" group by governance_type");

        //这个集合中的Map  是一行数据一个Map对象
        //Map中的key 是字段名 value是字段的值
        List<Map<String, Object>> listMaps = governanceAssessDetailService.listMaps(queryWrapper);

        //将查询出来的map集合的数据再转为新的map
        //新的map 的key 是老map grovernance_type这个key 对应的value
        //         value 是老map  ct 这个key 对应的value
        HashMap<String, Object> resultMap = new HashMap<>();
        for (Map<String, Object> map : listMaps) {
            resultMap.put((String) map.get("governance_type"), map.get("ct"));
        }

        return JSONObject.toJSONString(resultMap);
    }


    //分组人员排行榜
    @GetMapping("/rankList")
    public String rankList(){

        QueryWrapper<GovernanceAssessTecOwner> queryWrapper = new QueryWrapper<GovernanceAssessTecOwner>()
                .select("if(tec_owner is null,'没有技术负责人',tec_owner) as tecOwner", "score")
                .inSql("assess_date", "select max(assess_date) from governance_assess_tec_owner")
                .orderByDesc("score");
        List<Map<String, Object>> listMaps = governanceAssessTecOwnerService.listMaps(queryWrapper);

        return JSON.toJSONString(listMaps);
    }

}
